package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.MemberCommentsDao;
import com.xteam.ecsystem.entities.BaseReplyInfo;
import com.xteam.ecsystem.entities.BaseUserComment;
import com.xteam.ecsystem.utils.JsonDateValueProcessor;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

@Repository
@SuppressWarnings("unchecked")
public class MemberCommentsDaoImpl extends DDLPrefix implements
		MemberCommentsDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	@Override
	public String getMemberCommentsList(int pageIndex, int pageSize,
			String bucName, String bucType, String sbucTime, String ebucTime,
			String bucState) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			String hql = "select c.buc_id,c.buc_name,c.buc_email,c.buc_type,c.buc_target,c.buc_rank,c.buc_content,c.buc_ip,c.buc_time,c.buc_state,c.buc_title, r.bri_id from  base_user_comment c left join base_reply_info r on c.buc_id= r.bri_target where 1=1 ";
			String countHQL = "select count(*) from  base_user_comment c left join base_reply_info r on c.buc_id= r.bri_target where 1=1 ";
			if (bucName != null && bucName.length() > 0
					&& !bucName.equals("undefined") && !bucName.equals("null")) {
				hql += SQL_AND + " c.buc_name " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " buc_name " + SQL_LIKE + " ? ";
				list.add("%" + bucName + "%");
			}
			if (sbucTime != null && sbucTime.length() > 0
					&& !sbucTime.equals("undefined")
					&& !sbucTime.equals("null")) {
				hql += " and  c.buc_time>=?";
				countHQL += " and  c.buc_time>=?";
				list.add(StringUtil.strToDate(sbucTime, null));
			}
			if (ebucTime != null && ebucTime.length() > 0
					&& !ebucTime.equals("undefined")
					&& !ebucTime.equals("null")) {
				hql += " and  c.buc_time<=?";
				countHQL += " and  c.buc_time<=?";
				list.add(StringUtil.strToDate(ebucTime, null));
			}
			if (bucType != null && bucType.length() > 0
					&& !bucType.equals("undefined") && !bucType.equals("null")
					&& !bucType.equals("-1")) {
				hql += SQL_AND + " c.buc_type =? ";
				countHQL += SQL_AND + " c.buc_type =? ";
				list.add(Integer.parseInt(bucType));
			}
			if (bucState != null && bucState.length() > 0
					&& !bucState.equals("undefined")
					&& !bucState.equals("null") && !bucState.equals("-1")) {
				hql += SQL_AND + " c.buc_state =? ";
				countHQL += SQL_AND + " c.buc_state =? ";
				list.add(Integer.parseInt(bucState));
			}
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			// PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
			// .queryList(countHQL, hql, list.toArray(), (pageIndex - 1)
			// * pageSize, pageSize);
			PageCenterStation<?> p = baseDao.queryWithSQL(Map.class, countHQL,
					hql, null, list.toArray(), pageIndex * pageSize,
					pageSize);
			jo = JSONObject.fromObject(p, jsonConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jo.toString();
	}

	@Override
	public String getCommentsReply(String id) {
		String sql = "from BaseReplyInfo where briTarget='" + id + "'";
		List list = baseDao.find(sql);
		JSONObject jo = new JSONObject();
		if (list != null && list.size() > 0) {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			BaseReplyInfo rinfo = (BaseReplyInfo) list.get(0);
			jo = JSONObject.fromObject(rinfo, jsonConfig);
		}
		return jo.toString();
	}

	@Override
	public void updateComments(String id, String state) {
		String sql = "update  base_user_comment set buc_state="
				+ Integer.parseInt(state) + " where buc_id='" + id + "'";
		baseDao.executeUpdateWithSQL(sql, (Object[]) null);
	}

	@Override
	public void saveOrUpdateComments(Object obj, String id) {
		if (id != null && !"".equals(id) && !"undefined".equals(id)
				&& !"null".equals(id)) {
			baseDao.update(obj);
		} else {
			BaseReplyInfo cc = (BaseReplyInfo) obj;
			cc.setBriId(null);
			baseDao.save(cc);
		}
	}

	@Override
	public void delComments(String ids) {
		String oId[] = ids.split(",");
		
		for (String id : oId) {
			String sql = "delete from base_user_comment where buc_id='" + id + "'";
			String sqlReply = "delete from base_reply_info where bri_target='"
					+ id + "'";
			
			baseDao.executeUpdateWithSQL(sql, (Object[]) null);
			baseDao.executeUpdateWithSQL(sqlReply, (Object[]) null);
		}
	}

	@Override
	public String findCommentsById(String id) {
		String sql = " from BaseUserComment where bucId='" + id + "'";
		List list = baseDao.find(sql);
		JSONObject jo = new JSONObject();
		if (list != null && list.size() > 0) {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			BaseUserComment bucinfo = (BaseUserComment) list.get(0);
			jo = JSONObject.fromObject(bucinfo, jsonConfig);
		}
		return jo.toString();
	}

}
